﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 多图片合成GIF
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Select_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Multiselect = true
            };
            dialog.ShowDialog();

            if (flowLayoutPanel.Controls.Count > 0)
            {
                //foreach (var pictureBox in flowLayoutPanel.Controls)
                //{
                //    (pictureBox as PictureBox).Image.Dispose();
                //}
                try
                {
                    flowLayoutPanel.Controls.Clear();

                }
                catch { }
            }

            if (dialog.FileNames.Length > 0)
            {

                foreach (var filename in dialog.FileNames)
                {
                    try
                    {
                        var img = new Bitmap(filename);
                        var pictureBox = new PictureBox();
                        pictureBox.Image = img;
                        pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox.Width = 100;
                        pictureBox.Height = 100;
                        flowLayoutPanel.Controls.Add(pictureBox);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }

        private void Button_Create_Click(object sender, EventArgs e)
        {
            if (flowLayoutPanel.Controls.Count == 0)
            {
                MessageBox.Show("请先选择要合成的图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                string gifname = DateTime.Now.ToString("yyyyMMdd-HHmmss-fff") + ".gif";
                using (var gif = AnimatedGif.AnimatedGif.Create(gifname, int.Parse(textBox_Delay.Text.Trim())))
                {
                    foreach (var pictureBox in flowLayoutPanel.Controls)
                    {
                        gif.AddFrame((pictureBox as PictureBox).Image);
                    }
                }
                if (pictureBox_Result.Image != null)
                {
                    pictureBox_Result.Image.Dispose();
                }
                pictureBox_Result.Image = new Bitmap(gifname);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
